/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-03-20 Marco Riccardi - initial implementation
 *
 */
package org.mizartools.system.executable;

import java.io.File;
import java.util.LinkedList;

import org.mizartools.utility.IOutputAnalyzer;
import org.mizartools.utility.ProcessManager;
import org.mizartools.utility.TemporaryDirectory;

public abstract class AbstractExecutableFile {

	private boolean executing = false;
	private TemporaryDirectory temporaryDirectory;
	private String mizarFile;
	private ProcessManager processManager;
	protected long lengthMizarFile = 0;
	protected ExecutableFileEnum executableFileEnum;
	protected IOutputAnalyzer outputAnalyzer;
	
	public AbstractExecutableFile(TemporaryDirectory temporaryDirectory, String mizarFile) throws ExecutableException{
		if (temporaryDirectory.isLocked()) {
			throw new ExecutableException(ExecutableException.LOCKED_TEMPORARY_DIRECTORY, "The temporary directory [" + temporaryDirectory.getFile().getPath() + "] is locked.");
		}
		if (!mizarFile.toLowerCase().endsWith(".miz")) {
			throw new ExecutableException(ExecutableException.NOT_VALID_EXTENSION, "The file [" + mizarFile + "] has not a valid extension.");
		}
		File file = new File(temporaryDirectory.getFile(), mizarFile);
		if (!file.exists()) {
			throw new ExecutableException(ExecutableException.FILE_NOT_EXIST, "The file [" + file.getPath() + "] not exists.");
		}
		if (file.isDirectory()) {
			throw new ExecutableException(ExecutableException.MIZAR_FILE_IS_DIRECTORY, "[" + file.getName() + "] is a directory.");
		}
		this.lengthMizarFile = file.length();
		this.temporaryDirectory = temporaryDirectory;
		this.mizarFile = mizarFile;
	}
	
	public void start() throws ExecutableException {
		if (isExecuting()) {
			throw new ExecutableException(ExecutableException.IS_ALREADY_EXECUTING, "The job is already executing.");
		}
		else {
			setExecuting(true);
			Job job = new Job();
			Thread thread = new Thread(job);
			thread.start();
		}
	}
	
	public String getLastOutput(){
		if (this.processManager != null) {
			return this.processManager.getLastOutput();
		}
		else return "";
	}

	public int getExitValue(){
		if (this.processManager != null) {
			return this.processManager.getExitValue();
		}
		else return -1;
	}

	public Exception getException(){
		if (this.processManager != null) {
			return this.processManager.getException();
		}
		else return null;
	}
	
	private void setExecuting(boolean executing) {
		synchronized (this) {
			this.executing = executing;
			this.temporaryDirectory.setLocked(executing);
		}
	}

	public boolean isExecuting() {
		synchronized (this) {
			return executing;
		}
	}

	public abstract int getPercentage();
	
	private class Job implements Runnable {
		@Override
		public void run() {
			File workDirectory = temporaryDirectory.getFile();
    		ExecutableFileManager executableFileManager = ExecutableFileManager.getInstance();
    		LinkedList<String> commandList = new LinkedList<String>();
    		commandList.add(executableFileManager.getCommand(executableFileEnum));
    		commandList.add(mizarFile);
    		processManager = new ProcessManager(workDirectory, commandList);
    		processManager.setRedirectOutput(false);
    		processManager.setCaptureLastOutput(true);
    		processManager.setCaptureOutput(false);
    		processManager.setPriority(Thread.MIN_PRIORITY);
    		processManager.setProcessOutputAnalyzer(outputAnalyzer);
    		processManager.start();
    		processManager.waitProcess();
			setExecuting(false);
		}
	}
}
